package net.yebaihe.wc;

import java.util.HashMap;

import net.yebaihe.wc.DataMan.MSGTYPE;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public abstract class StateMachine extends Activity {

	LinearLayout topbar;
	private View client;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d("myown", this.getClass().getName());
        LinearLayout main=new LinearLayout(this);
        main.setOrientation(LinearLayout.VERTICAL);
        topbar=(LinearLayout) View.inflate(this, R.layout.topbar, null);
        updateTopBar();
        client=View.inflate(this, getLayoutRes(), null);
        main.addView(topbar);
        main.addView(client);
        setContentView(main);
        prepareClient(client);
    }
    
    private void updateTopBar() {
    	TextView txtTitle=(TextView) topbar.findViewById(R.id.txtTitle);
    	txtTitle.setText(getTopbarTitle());
    	
    	TextView txtLeft=(TextView)topbar.findViewById(R.id.txtleft);
    	Button btnRight=(Button)topbar.findViewById(R.id.btnRight);
    	
    	if ((this.getClass()==Login.class) || (this.getClass()==Index.class)) {
    		txtLeft.setText(getCurrentCity());
        	txtLeft.setOnClickListener(new OnClickListener(){
    			@Override
    			public void onClick(View arg0) {
    				Intent intent=new Intent(StateMachine.this,Cities.class);
    				startActivity(intent);
    				overridePendingTransition(R.anim.up_in,R.anim.up_out); 
    			}
        	});
    	}
    	else if ((this.getClass()==Register.class) || (this.getClass()==UserInfo.class) || (this.getClass()==Cities.class)){
    		btnRight.setVisibility(View.INVISIBLE);
    		txtLeft.setText("返回");
    		txtLeft.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					goback();
				}
    		});
    	}
    	else if (this.getClass()==IndexSub.class){
    		txtLeft.setText("首页");
    		txtLeft.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					goback();
    				//Intent intent=new Intent(StateMachine.this,Index.class);
    				//startActivity(intent);
    				//overridePendingTransition(R.anim.up_in,R.anim.up_out); 
				}
    		});
    	}
    	
	}

	private String getCurrentCity() {
		WCApp app=(WCApp)this.getApplicationContext();
		if (app.city.length()<=0){
			app.city="北京";
		}
		return app.city;
	}

	protected void goback() {
		super.onBackPressed();
	}

	protected abstract String getTopbarTitle();
	protected abstract void prepareClient(View client);
	protected abstract void HandleMessage(MSGTYPE register, boolean ret, HashMap<String, String> params);
    protected abstract int getLayoutRes();
	protected WCApp getWCAppContext() {
		return (WCApp)this.getApplicationContext();
	}


}